﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpScoreCard : CustBizObject
    {
        private CDmShpScoreCard m_CDmShpScoreCard = new CDmShpScoreCard();
        private CDmShpScoreCard oDmShpScoreCard
        {
            get { return m_CDmShpScoreCard; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpScoreCard()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpScoreCard.SHP_SCORE_CARD);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["SCORE_CARD_ID"]; 
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            if (drw["SCORE_CARD_NO"].ToString() == string.Empty)
            {
                drw["SCORE_CARD_NO"] = GetNewCardNo();// msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("TRADING_NO", System.DateTime.Now.Year.ToString(), 8);
            }
            //drw["IS_USE"] = false;
            //drw["SELLER_DEL"] = false;
            //drw["BUYER_DEL"] = false;
            //drw["BUYER_RATE_SID"] = "";

            return true;
        }
        protected override bool AfterRowAdded(DataRow drw)
        {
            using (CBizShpScoreTradingLog oscorelog = new CBizShpScoreTradingLog())
            {
                oscorelog.CreatScoreCardForShop(drw);
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            } 
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            if (drw["USE_DT"].ToString() !=string.Empty && drw["USE_DT"].ToString() != drw["USE_DT", DataRowVersion.Original].ToString())
            {
                using (CBizShpScoreTradingLog oscorelog = new CBizShpScoreTradingLog())
                {
                    oscorelog.UseScoreCardForUser(drw);
                }
            }
            return true;
        }
        public string GetNewCardNo()
        {
            return "04" + msBsc.SvRef.ApplicationConst.Const.GeneratelongId().ToString();
        }

        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT A.*  FROM " + this.TableName + " A   WHERE  A." + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByAccount(string strCreateNo,string strUseNo)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  (''='" + strCreateNo + "' OR CREATE_ACCOUNT_NO ='" + strCreateNo + "') AND (''='" + strCreateNo + "' OR USE_ACCOUNT_NO ='" + strUseNo + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
        public DataSet SelectByCardNo(string strCardNo)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  ( SCORE_CARD_NO ='" + strCardNo + "') ");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public void CreateScoreCard( int iScoreQty)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oshopinfo = new CBizShpCommercialTenantInfo())
            {
                int iuqty = oshopinfo.GetShopScoreAllowUseQty(msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0]);
                if (iuqty < iScoreQty)
                {
                    throw new Exception("商家积分账户可用积分太少不能完成当前操作！");
                }

                DataSet dscard = new CDmShpScoreCard();
                DataRow drnew = dscard.Tables[0].NewRow();
                drnew["SCORE_CARD_ID"] = this.DataObj.NewID();
                drnew["SCORE_CARD_NO"] = GetNewCardNo();
                drnew["CREATE_ACCOUNT_NO"] = msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0];
                drnew["CREATE_ACCOUNT_NME"] = msBsc.SvRef.UserInfo.OperDealerId.Split(',')[1];
                drnew["SCORE_QTY"] = iScoreQty;
                dscard.Tables[0].Rows.Add(drnew);
                this.Update(dscard);


            }
        }

        public void UseScoreCard(string strCardNo)
        {
            DataSet ds = SelectByCardNo(strCardNo);
            
            if (ds.Tables[0].Rows.Count <= 0)
            {
                throw new Exception("积分充值卡卡号不存在！");
            }
            if (ds.Tables[0].Rows[0]["USE_DT"].ToString() != string.Empty)
            {
                throw new Exception("当前积分充值卡卡号在本次操作前已经被充值，不能重复充值！");
            }

            ds.Tables[0].Rows[0]["USE_DT"] = System.DateTime.Now;
            ds.Tables[0].Rows[0]["USE_ACCOUNT_NO"] = msBsc.AppLogic.Cfg.BseUserInfo.Operator;
            ds.Tables[0].Rows[0]["USE_ACCOUNT_NME"] = msBsc.AppLogic.Cfg.BseUserInfo.Operator;
            this.Update(ds);
        }
    }
}
